<?php
import("ORG.Util.Session");
// 本类由系统自动生成，仅供测试用途
class ResourceAction extends Action {

   private function checkLogin(){			//判断是否登陆 否则切到登陆界面
		session_start();
		if($_SESSION['online']==null){
			$this -> redirect('Login/index');
			exit;
		}
		return true;
	}

	public function upload(){
		$this->login();
		if($_GET['id']==null)
			{$this->filelist();}
		else{
			$this->filelist();
			$this->editfile();
		}
		$this->display();
	}

	public function addfile() {
        if (!empty($_FILES)) {
            //如果有文件上传 上传附件
            $this->_upload();
            //$this->forward();
        }else{

		}

    }

    // 文件上传
    protected function _upload() {
        import("@.ORG.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'doc,docx,ppt,pptx,xls,xlsx,pdf,zip,rar');
        //设置附件上传目录
        $upload->savePath = './Public/Uploads/';
        //设置需要生成缩略图，仅对图像文件有效
        /*$upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
        //设置缩略图最大宽度
         $upload->thumbMaxWidth = '200,80';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '120,80';
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;*/
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            /*import("@.ORG.Image");
            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
            Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], '../Public/Images/logo2.png');
            $_POST['image'] = $uploadList[0]['savename'];*/
        }
        $model = M('file');
        //保存当前数据对象
		$data=array(
			'filename'=>$_POST['filename'],
			'ftype'=>$uploadList[0]['extension'],
			'uptime'=>time(),
			'type'=>$_POST['type']
			);
        $data['path'] = $uploadList[0]['savename'];
        $list = $model->add($data);
        if ($list !== false) {
            $this->success('上传成功！');
        } else {
            $this->error('上传失败!');
        }
    }
	private function filelist(){
		$file=M('file');
		import("ORG.Util.Page");//导入分页类
		$index=$file->count();
		$Page = new Page($index,10);//实例化分页类传入总记录数和每页显示的记录数
		$show = $Page->show();//分页显示输出
		//进行分页数据查询注意limit方法的参数要使用Page类的属性
		$list = $file->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();

		for($i=0;$i<count($list);$i++){
			$list[$i]['uptime'] = 	date("Y-m-d h:i",$list[$i]['uptime']);
		}
		$this->assign('list',$list);
		$this->assign('page',$show);//赋值分页输出
	}

	private function editfile(){
		$file=M('file');
		$cond['id']=$_GET['id'];
		$cont=$file->where($cond)->select();
		//dump($cont);
		$this->assign('cont',$cont);
	}


	//删除
	public function deletefile(){
		$article=M('file');
		$id=$_GET[id];
		$cond['id']=$id;
		$index=$article->where($cond)->select();
		$myfile='./Public/Uploads/'.$index[0]['path'];
		if(file_exists(iconv('utf-8','gbk',$myfile)))
		{
				$result=unlink(iconv('utf-8','gbk',$myfile));
				if($result)
				{
					$article->where($cond)->delete();
					$this->success('删除成功！');
				}
				else
				{
					$this->error('删除失败！');
				}
		}
		else
		{
				$article->where($cond)->delete();
				$this->success('文件不存在');
		}

	}



	public function top(){
		$id=$_GET['id'];
		$model=M('file');
		$cond['id']=$id;
		$model->where($cond)->setInc('istop');
		$this->success('成功！');
	}

	public function down(){
		$id=$_GET['id'];
		$model=M('file');
		$cond['id']=$id;
		$model->where($cond)->setDec('istop');
		$this->success('成功！');
	}

	public function addlink(){
		$this->login();
		if($_GET['id']==null){
			$this->linklist();
		}else{
			$this->linklist();
			$this->modlink();
		}

		$this->display();
	}

	public function newlink(){
		$this->login();
		if($_POST['url']==null){
			 $this->error('链接不能为空！');
		}
		$model = M('article');
        //保存当前数据对象
		$data=array(
			'title'=>$_POST['title'],
			'url'=>$_POST['url'],
			'author'=>$_SESSION['online'],
			'content'=>$_POST['content'],
			'pubtime'=>time(),
			'type'=>'友情链接',
			'image'=>$_POST['image']
			);
		if($_POST['id']!=null){
			$cond['id']=$_POST['id'];
			if(!empty($_POST['pubhot']))
			{
			$data['istop']=999;
			}
			$list=$model->where($cond)->setField($data);
		}
		else
		{
			$data['id']=$_POST['id'];
			if(!empty($_POST['pub']))
			{
				$list = $model->add($data);
			}
			else
			{
				$data['istop']=999;
				$list = $model->add($data);
			}

		}

		if ($list !== false) {
            $this->success('发布成功！');
        } else {
            $this->error('发布失败!');
        }
	}
	public function modlink(){
		$link=M('article');
		$cond['id']=$_GET['id'];
		$index=$link->where($cond)->select();
		//dump($index);
		$this->assign('link',$index);
	}
	private function linklist(){
		$this->login();//登陆
		$article=M('article');
		import("ORG.Util.Page");//导入分页类
		$cond['type']='友情链接';
		$index=$article->where($cond)->count();
		$Page = new Page($index,5);//实例化分页类传入总记录数和每页显示的记录数
		$show = $Page->show();//分页显示输出
		//进行分页数据查询注意limit方法的参数要使用Page类的属性
		$list = $article->where($cond)->order('istop DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		for($i=0;$i<count($list);$i++){
			$list[$i]['pubtime'] = 	date("Y-m-d h:i",$list[$i]['pubtime']);
		}
		$this->assign('list',$list);
		$this->assign('page',$show);//赋值分页输出
	}
	public function deletelink(){
		$link=M('article');
		$id=$_GET[id];
		$cond['id']=$id;
		$link->where($cond)->delete();
		$this->success('删除成功！');
	}


	public function login(){		//登陆
        $this->	checkLogin();
		$username=$_SESSION['online'];
		if($_SESSION['power']==0){
			$this->assign('username',$username);
			$this->assign('power','超级管理员');
		}
		if($_SESSION['power']==1){
			$this->assign('username',$username);
			$this->assign('power','管理员');
		}
    }
	public function logout()
	{
		$this->	checkLogin();
		Session::clear();
		$this->assign('jumpUrl','__APP__');
		$this->assign('waitSecond',2);
		$this->success('退出成功！');
	}
}